//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BernsteinPolynomial.h"
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::CBernsteinPolynomial::CBernsteinPolynomial (boost::shared_ptr<QuantLib::BernsteinPolynomial>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBernsteinPolynomial = NULL;
#endif
	_ppBernsteinPolynomial = &childNative;
    
}
Cephei::QL::Math::CBernsteinPolynomial::CBernsteinPolynomial (QuantLib::BernsteinPolynomial& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phBernsteinPolynomial = NULL;
#endif
	_ppBernsteinPolynomial = new boost::shared_ptr<QuantLib::BernsteinPolynomial> (&childNative);
    
    _BernsteinPolynomialOwner = owner;
    
}

Cephei::QL::Math::CBernsteinPolynomial::CBernsteinPolynomial (CBernsteinPolynomial^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBernsteinPolynomial = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBernsteinPolynomial = new boost::shared_ptr<QuantLib::BernsteinPolynomial> (copy->GetShared());
        
    }
}
Cephei::QL::Math::CBernsteinPolynomial::CBernsteinPolynomial (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBernsteinPolynomial = NULL;
#endif
	if (!t->IsSubclassOf(CBernsteinPolynomial::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::CBernsteinPolynomial::CBernsteinPolynomial (QuantLib::Handle<QuantLib::BernsteinPolynomial>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBernsteinPolynomial = &childNative;
	_ppBernsteinPolynomial = &static_cast<boost::shared_ptr<QuantLib::BernsteinPolynomial>>(childNative.currentLink());
    
    _BernsteinPolynomialOwner = owner;
}
Cephei::QL::Math::CBernsteinPolynomial::CBernsteinPolynomial (QuantLib::Handle<QuantLib::BernsteinPolynomial> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBernsteinPolynomial = &childNative;
	_ppBernsteinPolynomial = &static_cast<boost::shared_ptr<QuantLib::BernsteinPolynomial>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::CBernsteinPolynomial::CBernsteinPolynomial (QuantLib::BernsteinPolynomial childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBernsteinPolynomial = NULL;
#endif
	_ppBernsteinPolynomial = new boost::shared_ptr<QuantLib::BernsteinPolynomial> (new QuantLib::BernsteinPolynomial (childNative));
    
}
#endif

Cephei::QL::Math::CBernsteinPolynomial::~CBernsteinPolynomial ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppBernsteinPolynomial != NULL)
    {
	    delete _ppBernsteinPolynomial;
        _ppBernsteinPolynomial = NULL;
    }
}
Cephei::QL::Math::CBernsteinPolynomial::!CBernsteinPolynomial ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppBernsteinPolynomial != NULL)
    {
	    delete _ppBernsteinPolynomial;
    }
}
QuantLib::BernsteinPolynomial& Cephei::QL::Math::CBernsteinPolynomial::GetReference ()
{
    if (_ppBernsteinPolynomial == NULL) throw REFNEW NativeNullException ();
	return **_ppBernsteinPolynomial;
}
boost::shared_ptr<QuantLib::BernsteinPolynomial>& Cephei::QL::Math::CBernsteinPolynomial::GetShared ()
{
    if (_ppBernsteinPolynomial == NULL) throw REFNEW NativeNullException ();
	return *_ppBernsteinPolynomial;
}
QuantLib::BernsteinPolynomial* Cephei::QL::Math::CBernsteinPolynomial::GetPointer ()
{
    if (_ppBernsteinPolynomial == NULL) throw REFNEW NativeNullException ();
	return &**_ppBernsteinPolynomial;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BernsteinPolynomial>& Cephei::QL::Math::CBernsteinPolynomial::GetHandle ()
{
	if (_phBernsteinPolynomial == NULL)
	{
		_phBernsteinPolynomial = new Handle<QuantLib::BernsteinPolynomial> (*_ppBernsteinPolynomial);
	}
	return *_phBernsteinPolynomial;
}
#endif
bool Cephei::QL::Math::CBernsteinPolynomial::HasNative () 
{
	return (_ppBernsteinPolynomial != NULL);
}

Double Cephei::QL::Math::CBernsteinPolynomial::Get (UInt32 i, UInt32 n, Double x)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Natural _i = (QuantLib::Natural)ValueHelper::Convert (i); //a
        QuantLib::Natural _n = (QuantLib::Natural)ValueHelper::Convert (n); //a
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBernsteinPolynomial)->get ( _i,  _n,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

